/** \class TcpHeader
 * TcpHeader.h
 *
 *  Created on: Oct 16, 2008
 *      Author: chaosun
 */

#ifndef TCPHEADER_H_
#define TCPHEADER_H_
#include "iu_wiretap.h"
using namespace std;
extern int dflag;

/* TCP header */
struct sniff_tcp {
  u_short th_sport;       /* source port */
  u_short th_dport;       /* destination port */
  int th_seq;             /* sequence number */
  int th_ack;             /* acknowledgement number */
  u_char th_offx2;        /* data offset, rsvd */
#define TH_OFF(th)      (((th)->th_offx2 & 0xf0) >> 4)
  u_char th_flags;
#define TH_FIN 0x01
#define TH_SYN 0x02
#define TH_RST 0x04
#define TH_PUSH 0x08
#define TH_ACK 0x10
#define TH_URG 0x20
#define TH_ECE 0x40
#define TH_CWR 0x80
#define TH_FLAGS (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
  u_short th_win;         /* window */
  u_short th_sum;         /* checksum */
  u_short th_urp;         /* urgent pointer */
};

class TcpHeader
{
public:
  static int n_FIN;
  static int n_ACK;
  static int n_SYN;
  TcpHeader();
  friend class TcpHeaderTest;
  /**
   * AlreadyIn. Check if the new TCP port has been added to the vector.
   * \param tcp_ is a pointer to the TCP header struct
   * \param portType indicates what kind of port to deal with; 0 for source and 1 for destination
   * \return the position of the existing TCP port, or -1 if it is not in the vector.
   */
  static int alreadyIn(struct sniff_tcp *tcp_, int portType);
  /**
   * countFlagType. Count the number of packets with the SYN, ACK, FIN set
   * \param flag is the TCP flag bits of a given header
   */
  static void countFlagType(u_char flag);
  /**
    * Packet handler for TCP Header
    * \param packet is a pointer for the whole packet
    * \param index is an index which indicate start of TCP header
    * \return next protocol
    */
  static Protocol handlePacket(const u_char * packet, int& index);
  string printTCP();
  static string showAll();
  virtual
  ~TcpHeader();
private:
  uint16_t tcpPort_;
};

static vector <TcpHeader> tcpVector;

#endif /* TCPHEADER_H_ */
